package com.wframe.project.jnd.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.frame.database.PageResult;
import com.wframe.project.jnd.services.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.frame.database.JdataBean;
import com.qiniu.util.Auth;
import com.wframe.project.jnd.extinterface.qiniu.QiniuConfig;
import com.wframe.project.jnd.rest.util.JsonReturn;
import com.wframe.project.jnd.rest.util.WebSession;
import com.wframe.project.jnd.util.WebUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    UserService userService;
    @Autowired
    FloorService floorService;
    @Autowired
    InformationService informationService;
    @Autowired
    OrderService orderService;
    @Autowired
    ProductService productService;
    @Autowired
    CacheMasterDataService cacheMasterDataService;
    @Autowired
    PromotionService promotionService;

    /**
     * 进入首页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "home")
    public String home(HttpSession session, Model model) throws Exception {
        if (WebSession.checkLoginStatus(session)) {
            String userid = WebSession.getWbUser(session).getUserid();
            model.addAttribute("user", userService.getUser(userid).getDataMap());
            model.addAttribute("ordernum", orderService.statStatusNum(userid));
            model.addAttribute("usernum", userService.statUserPro(userid, UserService.UserSourceEnum.PC.ordinal()).getDataMap());
        }

        JdataBean parm = new JdataBean();
        parm.put("code", "003001");
        model.addAttribute("banners", informationService.queryInfos(parm, 10, 1).getList());//加载首页滚动广// 告图


//        List<JdataBean> list = informationService.getIssueCode("003201");
//        List<Map> datas = new ArrayList<>();
//        for(int i = 0; i < list.size(); i++){
//            datas.add(list.get(i).getDataMap());

//        }
//        model.addAttribute("list",datas);
        return "/website/home";
    }

    @RequestMapping(value = "flashSales")
    public void loadFlashSales(Integer pageno, HttpSession session, HttpServletResponse response) {
        JsonReturn json = new JsonReturn();
        int pn = 1;
        if (pageno != null && pageno > 0) {
            pn = pageno.intValue();
        }
        Long uid = null;
        if (WebSession.checkLoginStatus(session)) {
            String userid = WebSession.getWbUser(session).getUserid();
            if (StringUtils.isNotEmpty(userid) && userid.trim().matches("^\\d+$")) {
                uid = Long.valueOf(userid.trim());
            }
        }
        try {
            PageResult pr = this.promotionService.findOnFlashSaleByPlatform(1, uid , 5, pn);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("pageno", pr.getPageNo());
            data.put("pagesize", pr.getPageSize());
            data.put("pagecount", pr.getPageCount());
            data.put("resultcount", pr.getResultCount());
            data.put("list", pr.getList());
            json.putData(data);
            WebUtils.responseWrite(response, json.toString());
        } catch (Exception e) {
            WebUtils.responseWrite(response, json.toErrorString(this.getClass(), e));
        }
    }

    /**
     * 楼层信息
     *
     * @return
     */
    @RequestMapping(value = "floors")
    public String floors(Model model, HttpSession session) {
        if (WebSession.checkLoginStatus(session)) {
            model.addAttribute("list", floorService.findHomeFloor(WebSession.getWbUser(session).getUserid()));
        } else {
            model.addAttribute("list", floorService.queryHomeFloor());
        }

        return "/website/home/floors";
    }

    /**
     * 楼层信息
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "floorproducts")
    public void floorproducts(String itemid, HttpServletResponse response) {
        JsonReturn json = new JsonReturn();
        try {
            json.putData(floorService.queryFloorProduct(null, itemid, 10, 1).getList());
            WebUtils.responseWrite(response, json.toString());
        } catch (Exception e) {
            WebUtils.responseWrite(response, json.toErrorString(this.getClass(), e));
        }
    }

    /**
     * 加载分类导航
     *
     * @return
     */
    @RequestMapping(value = "loadClsNav")
    public String loadClasNav(Model model) {
        model.addAttribute("list", cacheMasterDataService.loadClsNav("1"));
        return "/website/home/clsNav";
    }

    /**
     * 获取七牛云存储Token
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "qiniuToken")
    public void qiniuToken(HttpServletResponse response) {
        JsonReturn json = new JsonReturn();
        try {
            Auth auth = Auth.create(QiniuConfig.getConfig().getAccessKey(), QiniuConfig.getConfig().getSecretKey());
            String upToken = auth.uploadToken(QiniuConfig.getConfig().getBucket());
            WebUtils.responseWrite(response, "{\"uptoken\":\"" + upToken + "\"}");
        } catch (Exception e) {
            WebUtils.responseWrite(response, json.toErrorString(this.getClass(), e));
        }
    }
}
